package com.estar.app.business.mobileLogin.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


@SuppressWarnings({ "restriction", "unused" })
public class ImgUtils {
	private static final Logger logger = Logger.getLogger(ImgUtils.class);
	public static boolean GenerateImage(String picData, String imgFilePath) {
		if (picData == null) // 图像数据为空
			return false;
		
		BASE64Decoder decoder = new BASE64Decoder();
		OutputStream out = null;
		FileInputStream inputStream = null;
		File f = null;
		try {
			// Base64解码
			byte[] bytes = decoder.decodeBuffer(picData);
			for (int i = 0; i < bytes.length; ++i) {
				if (bytes[i] < 0) {// 调整异常数据
					bytes[i] += 256;
				}
			}
			// 生成原图片
			out = new FileOutputStream(imgFilePath);
			// 加水印方式
			out.write(bytes);
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			logger.error("系统异常：",e);
			return false;
		} finally {
			try {
				if (out != null){
					out.close();
				}
				if (inputStream != null){
					inputStream.close();
				}
			} catch (IOException e) {
				logger.error("系统异常：",e);
			}
		}
	}
}
